#include ".\image.h"
#include "..\clientinterfaces.h"
#include "iltdrawprim.h"
#include <iltmath.h>
image::image()
{
		m_tex=NULL;
		m_angle=0;
		m_rotate=false;
}
image::~image(void)
{
	if (m_tex)
	{
		g_pLTCTexInterface->ReleaseTextureHandle(m_tex);
		m_tex=NULL;
	}
	
}

void image::set_tex( HTEXTURE _tex )
{
	m_tex=_tex;
}

/******************************
verts[0]        verts[1]
_______________
|				    |
|     center			|
|_______________|
verts[3]		verts[2]
******************************/


void image::set_uv( float umin/*=0.0f*/,float vmin/*=0.0f */,float umax/*=1.0f*/,float vmax/*=1.0f*/ )
{
	m_u=umin;
	m_v=vmin;
	m_umax=umax;
	m_vmax=vmax;
	zuo_image::g_set_uv(m_poly,umin,vmin,umax,vmax);

}

void image::set_pos( float x/*=0.0f*/,float y/*=0.0f*/ )
{
	m_x=x;
	m_y=y;
	m_rotate=true;


}

void image::set_size( float wide/*=100.0f*/,float height/*=100.0f*/ )
{
	m_w=wide;
	m_h=height;
	zuo_image::g_set_xywh(m_base_poly,m_x,m_y,m_w,m_h);
	m_rotate=true;
	


}

void image::easy_set( float x/*=0.0f*/,float y/*=0.0f */,float wide/*=100.0f*/,float height/*=100.0f*/ )
{
	set_pos(x,y);
	set_size(wide,height);
	set_uv();
	set_color();
	m_rotate=true;

}

void image::set_color( int a/*=255*/,int r/*=255*/,int g/*=255*/,int b/*=255*/ )
{
	zuo_image::g_set_color(m_poly,a,r,g,b);

}

void image::render()
{
	if (m_rotate)
	{
		do_rotate();
		m_rotate=false;
	}
	g_pLTCDrawPrim->SetTexture(m_tex);
	g_pLTCDrawPrim->DrawPrim(&m_poly);

}

void image::scale( float x,float y )
{
	m_w*=x;
	m_h*=y;
	zuo_image::g_set_xywh(m_poly,m_x,m_y,m_w,m_h);

}

bool image::if_mouse_in( float x,float y )
{
	if ((x>=m_x)&&(x<=m_x+m_w))
	{
		if( (y>=m_y)&&(y<=m_y+m_h))
		{
			return true;
		}
	}
	return false;

}

void image::rotate( float _an )
{
	m_angle+=-_an;
}

void image::do_rotate()
{
	
	for (int i=0;i<4;++i)
	{
		float rx,ry;
		rx =m_base_poly.verts[i].y*sin(m_angle)+m_base_poly.verts[i].x*cos(m_angle);
		ry=m_base_poly.verts[i].y*cos(m_angle)+(-m_base_poly.verts[i].x*sin(m_angle));
		m_poly.verts[i].x=rx+m_x;
		m_poly.verts[i].y=m_y-ry;
	}



}

void image::set_rotate( float _an )
{
	m_angle=_an;
	m_rotate=true;


}

